//获取数据库
const url = "http://10.62.27.21/apim/";
// const url="/apim/";
getDBInfo();
async function getDBInfo() {
  const res = await request.get(`${url}source/sourceinfo`);
  showDB(res.data.data);
}

//废弃的ajax请求
function getSourceInfo() {
  const url = "https://10.62.4.147:53001/source/sourceinfo";
  $.ajax({
    url: url,
    contentType: "application/json",
    // headers: {
    //     // Authorization: 'Bearer ' + token,
    // },
    data: null,
    dataType: "json",
    success: function (result) {
      console.log(result);
      console.log($("p.lead"));
    },
    error: function (e) {
      alert("请求失败");
    },
  });
}

//动态显示数据库
// console.log($("div.marketing"))
const database = document.querySelector("div.marketing");
// console.log(database)
function showDB(DBInfo) {
  for (let i = 0; i < DBInfo.length; i++) {
    const div = document.createElement("div");

    div.classList.add("container", "col-xxl-10", "px-1", "py-3");
    div.innerHTML = `
        <div class="row align-items-center g-5 py-3 xx ">
        <div class="col-10 col-sm-8 col-lg-6 animation">
            <div class="baseimg" style="background-image: url(./assets/img/ele/data2.png);"></div>
            <div class="overlay">
        </div>
        </div>
            <div class="col-lg-6">
                <h3 class="lh-1 mb-3">${DBInfo[i].sourcename}</h2>
                <p class="lead "><strong>类型：</strong>${
                  DBInfo[i].source_type_name
                }
                            <br><strong>容量：</strong>${calc(
                              DBInfo[i].totalsize
                            )}
                            <br><strong>元数据量：</strong>${
                              DBInfo[i].metacount
                            }
                            <br><strong>文件总数：</strong>${
                              DBInfo[i].filecount
                            }
                            <br><strong>时相跨度：</strong>${dateSpan(
                              DBInfo[i].mintime,
                              DBInfo[i].maxtime
                            )}
                            <br><strong>更新时间：</strong>${dateTransform(
                              DBInfo[i].lasttime
                            )}
                            <br><strong>详情：</strong>${
                              DBInfo[i].description
                            }</p>
            </div>
        </div>
        </div>
        `;
    database.appendChild(div);
  }
}

//时间
function dateTransform(time) {
  if (time !== "") {
    const date = new Date(time); //转换为Date对象
    console.log(time);
    console.log(date);
    const year = date.getFullYear();
    const month = date.getMonth() + 1; // 月份从0开始，需要加1
    const day = date.getDate();
    const hours = date.getHours();
    const minutes = date.getMinutes();
    const seconds = date.getSeconds();

    const formattedDate =
      year +
      "." +
      month +
      "." +
      day +
      " " +
      hours +
      ":" +
      minutes +
      ":" +
      seconds;
    return formattedDate;
  } else {
    return "暂无时间";
  }
}

//时间跨度
function dateSpan(startTime, endTime) {
  if (startTime !== "" && endTime !== "") {
    const startDate = new Date(startTime); //转换为Date对象
    const endDate = new Date(endTime);

    const formattedDate =
      startDate.getFullYear() +
      "." +
      (startDate.getMonth() + 1) +
      "." +
      startDate.getDate() +
      " " +
      startDate.getHours() +
      ":" +
      startDate.getMinutes() +
      ":" +
      startDate.getSeconds() +
      "-" +
      endDate.getFullYear() +
      "." +
      (endDate.getMonth() + 1) +
      "." +
      endDate.getDate() +
      " " +
      endDate.getHours() +
      ":" +
      endDate.getMinutes() +
      ":" +
      endDate.getSeconds();
    return formattedDate;
  } else {
    return "暂无时间";
  }
}

//时间差
function dateLength(startTime, endTime) {
  if (startTime !== "" && endTime !== "") {
    const mintime = new Date(startTime);
    const maxtime = new Date(endTime);
    const timespan = maxtime.getTime() - mintime.getTime(); //getTime() 方法获取时间节点的时间戳（以毫秒表示）
    // const milliseconds = timespan % 1000;
    // const seconds = Math.floor((timespan / 1000) % 60);
    // const minutes = Math.floor((timespan / (1000 * 60)) % 60);
    // const hours = Math.floor((timespan / (1000 * 60 * 60)) % 24);
    const days = Math.floor(timespan / (1000 * 60 * 60 * 24));

    const formattedTimespan = days + "天 ";
    console.log(formattedTimespan);
    return formattedTimespan;
  } else {
    return "暂无时间";
  }
}

// 计算内存
function calc(data) {
  if (data < 1024) {
    return `${data.toFixed(2)} B`;
  } else if (data < 1024 ** 2) {
    return `${(data / 1024).toFixed(2)} KB`;
  } else if (data < 1024 ** 3) {
    return `${(data / 1024 ** 2).toFixed(2)} MB`;
  } else if (data < 1024 ** 4) {
    return `${(data / 1024 ** 3).toFixed(2)} GB`;
  } else if (data < 1024 ** 5) {
    return `${(data / 1024 ** 4).toFixed(2)} TB`;
  } else {
    return `${(data / 1024 ** 5).toFixed(2)} PB`;
  }
}
